package com.googlecode.flightreports.citydetails;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.googlecode.kohmori.springmvc.SimpleReportController;
import javax.servlet.http.HttpSession;

public class ReportController extends SimpleReportController
{
    @Override
    protected Object formBackingObject(HttpServletRequest arg0) throws Exception {
        ReportParams form = (ReportParams) super.formBackingObject(arg0);
//      form.setCreateSourceXMLFile(true);  // debugging only
        return form;        
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws ServletException {
        Map<String, List> refData = new HashMap<String, List>();
        
        // obtain DB connection and parameter information
        HttpSession session = request.getSession(true);
        conn = (Connection) session.getAttribute("connection");

        String sqlStatement = "select distinct city_name " +
                "from APP.CITIES order by city_name ";

        ArrayList<String> cityList = new ArrayList<String>();
        try {
            PreparedStatement pstmt = conn.prepareStatement(sqlStatement);            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                cityList.add(rs.getString("city_name"));
            }
            rs.close();
            pstmt.close();
            refData.put("cityList", cityList);
        } catch (SQLException e) {
            throw new ServletException("refData error: " + e.getMessage());
        }
               
        return refData;
    }
}
